<template>
    <div>
        <!--面包屑导航-->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!--卡片视图区域-->
        <el-card>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入关键字" v-model="queryInfo.query" clearable @clear="getGoodsList">
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="goAddPage">添加商品</el-button>
                </el-col>
            </el-row>
        </el-card>
        <!--表格区域-->
        <el-table :data="goodList" style="width: 100%" border stripe>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column prop="goods_name" label="商品名称"></el-table-column>
            <el-table-column prop="goods_price" label="商品价格(元)" width="180"></el-table-column>
            <el-table-column prop="goods_weight" label="商品重量" width="180"></el-table-column>
            <el-table-column prop="add_time" label="创建时间" width="180px">
                <template slot-scope="scope">
                    {{ scope.row.add_time | dateFormat }}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="180px">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoods(scope.row.goods_id)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[20, 40, 100, 200,400]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>


    </div>
</template>

<script>
    export default {
        data(){
            return {
                queryInfo : {
                    query : '',
                    pagenum : 1,
                    pagesize : 20
                },
                //商品列表
                goodList : [],
                //总数
                total : 0
            }
        },
        created() {
            this.getGoodsList();
        },
        methods:{
            async getGoodsList(){
                const {data : res} = await this.$http.get('goods',{params:this.queryInfo});
                if(res.meta.status !== 200){
                    return this.$message.error('商品列表获取失败');
                }
                this.goodList = res.data.goods;
                this.total = res.data.total;
            },
            //修改每页显示的条数
            handleSizeChange(newSize){
                //将每页显示的条数发生改变之后
                this.queryInfo.pagesize = newSize;
                //进行数据重载
                this.getGoodsList();
            },
            //修改当前的页码数
            handleCurrentChange(current){
                //将当前页的数据发生变化之后然后进行数据重载
                this.queryInfo.pagenum = current;
                this.getGoodsList();
            },
            //删除商品
            async removeGoods(id){
                const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(err => err);
                if(confirmResult === 'cancel'){
                    return this.$message.warning('已经取消了删除');
                }
                //进行删除操作
                const {data : res} = await this.$http.delete(`goods/${id}`);
                if(res.meta.status !== 200){
                    return this.$message.error('删除产品失败');
                }
                this.$message.success('删除商品成功');
                //商品删除完成之后,进行数据的重载
                this.getGoodsList();
            },
            goAddPage(){
                this.$router.push('/goods/add');
            }
        },
        computed:{

        }
    }
</script>

<style scoped>
.el-card{
    margin-top: 10px;
}
.el-table{
    margin-top: 10px;
}
</style>
